
import '../../sass/auth/ManagerEdit.scss';
import router from '../../router/Index.js';

export default {
    data(){
        let checkUsername = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('用户名不能为空'));
            }
            let str = /^[A-Za-z0-9]*$/;
            if (!str.test(value)) {
                callback(new Error('请输入字母、数字'));
            } else {
                if (value.length < 4) {
                    callback(new Error('请至少输入4个字符'));
                } else {
                    callback();
                }
            }
        };
        let checkUsernameDsp = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('昵称不能为空'));
            }
            if (value.length < 2) {
                callback(new Error('请至少输入2个字符'));
            } else {
                callback();
            }
        };
        let validatePass = (rule, value, callback) => {
            if(this.id <=0){
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (value.length < 6 || value.length > 16) {
                        callback(new Error('密码长度为6~16位'));
                    }
                    if (this.ManagerForm.checkPass !== '') {
                        this.$refs.ManagerForm.validateField('checkPass');
                    }
                    callback();
                }
            }else{
                if(value !== ''){
                    if (value.length < 6 || value.length > 16) {
                        callback(new Error('密码长度为6~16位'));
                    }
                }
                if (this.ManagerForm.checkPass !== '' && value !== this.ManagerForm.checkPass) {
                    callback(new Error('两次输入密码不一致!'));
                }
                callback();
            }
        };
        let validatePass2 = (rule, value, callback) => {
                if(this.id <=0) {
                    if (value === '') {
                        callback(new Error('请再次输入密码'));
                    } else if (value !== this.ManagerForm.password) {
                        callback(new Error('两次输入密码不一致!'));
                    } else {
                        callback();
                    }
                }else{
                    if (this.ManagerForm.password!=='' && value !== this.ManagerForm.password) {
                        callback(new Error('两次输入密码不一致!'));
                    } else {
                        callback();
                    }
                }
        };
        return {
            id:0,
            type:'添加',
            labelPosition: 'left',
            loading:false,
            userData:'',
            ManagerForm: {
                password: '',
                checkPass: '',
                username: '',
                username_dsp:'',
                status:false
            },
            rules: {
                password: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                username: [
                    { validator: checkUsername, trigger: 'blur' }
                ],
                username_dsp: [
                    { validator: checkUsernameDsp, trigger: 'blur' }
                ],
            }
        }
    },
    components:{

    },
    created:function () {
        if(this.$route.query.id) {
            this.id = this.$route.query.id;
            this.type = '编辑';
            this.fetchData();
        }
    },
    computed: {

    },
    filters:{

    },
    methods: {
        submitForm(formName) {
            let _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true;
                    if(_this.id > 0 ){
                        var data = {
                            username:_this.ManagerForm.username,
                            password:_this.ManagerForm.password,
                            username_dsp:_this.ManagerForm.username_dsp,
                            check_pass:_this.ManagerForm.checkPass,
                            status:_this.ManagerForm.status,
                            id: _this.id
                        };
                        var request_url = "/backend/user/editUser";
                    }else{
                        var data = {
                            username:_this.ManagerForm.username,
                            password:_this.ManagerForm.password,
                            username_dsp:_this.ManagerForm.username_dsp,
                            check_pass:_this.ManagerForm.checkPass,
                            status:_this.ManagerForm.status
                        };
                        var request_url = "/backend/user/addUser";
                    }
                    $.ajax({
                        type:"POST",
                        url: request_url,
                        data:data,
                        dataType:"json",
                        success:function(result){
                            if(result.code===0){
                                _this.$message({
                                    message: result.msg,
                                    type: 'success'
                                });
                                _this.loading = false;
                                _this.backToManager();
                            }else if(result.code ===-1){
                                _this.$message({
                                    message: result.msg,
                                    type: 'warning'
                                });
                            }else if(result.code ===-2){
                                _this.$message({
                                    message: result.msg,
                                    type: 'error'
                                });
                            }else if(result.code ===-999){
                                _this.$message({
                                    message: result.msg,
                                    type: 'error'
                                });
                                setTimeout(function () {
                                    window.location.reload();
                                },1500);
                            }
                            _this.loading = false;
                        },
                        error:function () {
                            _this.$message({
                                message: '调用接口失败',
                                type: 'error'
                            });
                            _this.loading = false;
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            if(this.id <=0)
                this.$refs[formName].resetFields();
            else
                this.setParams(this.userData);
        },
        backToManager(){
            router.push({path:'/auth/manager'});
        },
        async fetchData() {
            let _this=this,response,_response;
            let url = '/backend/user/user';
            this.loading = true;
            try {
                response = await fetch(url, {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-type': "application/json",
                    },
                    credentials: 'same-origin',
                    body:JSON.stringify({
                        'id' : _this.id,
                    })
                });
                _response = await response.json();

                if(_response.code===0){
                    _this.userData = _response.data;
                    _this.setParams(_this.userData);
                }else if(_response.code ===-2){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                }else if(_response.code ===-999){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                    setTimeout(function () {
                        window.location.reload();
                    },1500);
                }
                _this.loading = false;
            } catch(err) {
                _this.$message({
                    message: '调用接口失败',
                    type: 'error'
                });
                _this.loading = false;
            }
        },
        setParams(data) {
            let _this = this;
            _this.ManagerForm.username = data.username;
            _this.ManagerForm.username_dsp = data.username_dsp;
            if (data.status == 1) {
                _this.ManagerForm.status = true;
            } else {
                _this.ManagerForm.status = false;
            }
        }
    }
}